﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using WGM_Client_Net35V2.HelperClasses;
using WGM_Client_Net35V2.Properties;
using WGM_Client_Net35V2.Controls;

namespace WGM_Client_Net35V2.Views
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public TabItem MainTab { get { return _mainTab; } }
        private WGM_Svc.ServiceSoapClient _client = new WGM_Client_Net35V2.WGM_Svc.ServiceSoapClient();
        public WGM_Svc.ServiceSoapClient WGMClient { get { return _client; } }
        public MainWindow()
        {
            ClassRegistry.AddClass(this);    
            InitializeComponent();
            Settings.Default.LastMacAddress = NetworkManager.GetMacAddress();
            WGMClient.GetNetworksCompleted += new EventHandler<WGM_Client_Net35V2.WGM_Svc.GetNetworksCompletedEventArgs>(WGMClient_GetNetworksCompleted);
            WGMClient.JoinNetworkCompleted += new EventHandler<WGM_Client_Net35V2.WGM_Svc.JoinNetworkCompletedEventArgs>(WGMClient_JoinNetworkCompleted);
        }

        void WGMClient_JoinNetworkCompleted(object sender, WGM_Client_Net35V2.WGM_Svc.JoinNetworkCompletedEventArgs e)
        {
            MainView mainView = ClassRegistry.GetClass(typeof(MainView)) as MainView;
            mainView.SetBusy(false);
            if (e.Error == null)
            {
                if (e.Result != null)
                {
                    (ClassRegistry.GetClass(typeof(PlacesView)) as PlacesView).SetCurrentPlace(e.UserState as Place);
                    ShowAlert("Building contact list...");
                }
                else
                {
                    ShowAlert(Colors.DarkViolet, e.message);
                }
            }
            else
            {
                ShowAlert(Colors.Red, "Error Contacting Server...");
            }
        }

        void WGMClient_GetNetworksCompleted(object sender, WGM_Client_Net35V2.WGM_Svc.GetNetworksCompletedEventArgs e)
        {
            ClearAlert();            
            (ClassRegistry.GetClass(typeof(MainView)) as MainView).IsEnabled = true;
            if (e.Error == null)
            {
                if (e.Result != null)
                {
                    foreach (var placeData in e.Result)
                    {                        
                        (ClassRegistry.GetClass(typeof(PlacesView)) as PlacesView).AddPlace(placeData);
                    }
                }
                else
                {
                    ShowAlert(Colors.DarkSlateGray, e.message);
                }
            }
            else
            {
                ShowAlert(Colors.Red, "Error Contacting Server...");
            }
        }
        public void ShowAlert(string text)
        {
            ShowAlert(Colors.DarkSlateGray, text);
        }
        public void ShowAlert(Color colour, string text)
        {
            _alertBorder.Background = new SolidColorBrush(colour);
            _alertMessageTB.Text = text;
            _alertMessageTB.Measure(new Size(Double.PositiveInfinity,double.PositiveInfinity));
            _alertMessageRow.Height = new GridLength(_alertMessageTB.DesiredSize.Height);
        }
        public void ClearAlert()
        {
            _alertMessageTB.Text = "";

            _alertMessageRow.Height = new GridLength(0);
        }

        public void RetrievePlaces()
        {            
            ShowAlert(Colors.Black, "Retrieving Places...");
            (ClassRegistry.GetClass(typeof(MainView)) as MainView).IsEnabled = false;
            WGMClient.GetNetworksAsync(Settings.Default.LastLoginName, Settings.Default.LastLoginPassword);
        }
    }
}
